// pages/batchAddRoomsConfirm/batchAddRoomsConfirm.js
var that = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    houseData: {}
  },

  floorChack: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    that.data.houseData.floors[index].check = !that.data.houseData.floors[index].check;
    for (var i in that.data.houseData.floors[index].rooms) {
      that.data.houseData.floors[index].rooms[i].check = that.data.houseData.floors[index].check;
    }
    this._setData();
  },
  // 点击每个房间
  roomsCkeck: function (e) {
    var index = e.currentTarget.dataset.index;
    var idx = e.currentTarget.dataset.idx;
    console.log(index)
    that.data.houseData.floors[index].rooms[idx].check = !that.data.houseData.floors[index].rooms[idx].check;
    for (var i in that.data.houseData.floors[index].rooms) {
      //如果已经是选择就并有一个取消就取消全选
      if (that.data.houseData.floors[index].check && !that.data.houseData.floors[index].rooms[idx].check) {
        that.data.houseData.floors[index].check = false;
      }
      //判断有一个不一样就跳出循环
      if (that.data.houseData.floors[index].rooms[i].check != that.data.houseData.floors[index].rooms[idx].check) {
        break;
      }//全部一样就为全选
      if (i == that.data.houseData.floors[index].rooms.length - 1) {
        that.data.houseData.floors[index].check = that.data.houseData.floors[index].rooms[idx].check
      }

    }
    this._setData();
  },
  //下一步
  next: function () {
    wx.showLoading({
      title: '加载中...',
    })
    //复制出新的对象
    var house = this.data.houseData;
    // 先缓存到本地
    wx.setStorageSync('roomsh', house);
    //删除用户取消的房号
    if (house.floors){
      var sum = house.floors.length - 1;//房间的总数
      for (var i = sum; i > -1; i--) {
        var d = house.floors[i].rooms.length - 1;//删除总计
        for (var j = d; j > -1; j--) {
          if (!house.floors[i].rooms[j].check) {
            house.floors[i].rooms.splice(j, 1);

          }
        }

      }
    }
    //把check全部变为false 为了添加配置的时候使用
    if (house.floors) {
      for (var i in house.floors) {
        house.floors[i].check = false;
        for (var j in house.floors[i].rooms) {
          house.floors[i].rooms[j].check = false;
        }
      }
    }
   
    //取出缓存给对象
    this.initdata();
    wx.hideLoading();
    wx.setStorageSync('rooms', house);
    console.log(house)
    wx.navigateTo({
      url: '../batchAddConfigList/batchAddConfigList',
    })
  },
  _setData: function () {
    that.setData({
      houseData: that.data.houseData
    })
  },

  initdata:function(){
    var house = wx.getStorageSync('roomsh');
    wx.removeStorageSync('roomsh')
    if (house) {
      this.setData({
        houseData: house,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    that = this;
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initdata();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})